/*
 * Timeloit.com Inc.
 * Copyright (c) 2012 时代凌宇物联网数据平台. All Rights Reserved
 */
package org.sphx.api;

import java.util.Map;

import lombok.Setter;

/**
 * @author wangzx $Id$
 * 
 */
public class SphinxUtil {
    
     @Setter
     String host;
     @Setter
     int port;
     public static int MODE=SphinxClient.SPH_MATCH_BOOLEAN;
     public static int SORT_MODE=SphinxClient.SPH_SORT_EXTENDED;
    /**
     * 根据查询参数返回结果集
     * @param map
     * @return
     */
    public  SphinxResult getSphinxResult(Map map){
        SphinxClient cl = new SphinxClient();
        int offset = (Integer)map.get("start");
        int limit =  (Integer)map.get("limit");
        String index = (String) map.get("index");
        String sortClause =  map.get("sortClause") ==null ? "" : (String)map.get("sortClause");
        String groupBy =  map.get("groupBy") == null ? "" : (String)map.get("groupBy");
        String groupSort =  map.get("groupSort") ==null ? "" : (String) map.get("groupSort") ;
        
        int model = (Integer) map.get("model");
        int sortModel = (Integer) map.get("sortModel");
        SphinxResult res=null;
        
        try {
            cl.SetServer ( host, port );
            cl.SetWeights ( new int[] { 100, 1 } );
            cl.SetMatchMode ( model );
            cl.SetLimits ( offset, limit );
            cl.SetSortMode ( sortModel, sortClause );
            if ( groupBy.length()>0 )
                cl.SetGroupBy ( groupSort, SphinxClient.SPH_GROUPBY_ATTR, groupSort );

             res = cl.Query((String)map.get("queryStr"), index);
             if ( res==null )
             {
                 System.err.println ( "Error: " + cl.GetLastError() );
                 System.exit ( 1 );
             }
             if ( cl.GetLastWarning()!=null && cl.GetLastWarning().length()>0 )
                 System.out.println ( "WARNING: " + cl.GetLastWarning() + "\n" );
             //System.out.println ( "\nMatches:" );
             for ( int i=0; i<res.matches.length; i++ )
             {
                 SphinxMatch info = res.matches[i];
                 //System.out.print ( (i+1) + ". id=" + info.docId + ", weight=" + info.weight +" \n");

                 if ( res.attrNames==null || res.attrTypes==null )
                     continue;
             }
        } catch (SphinxException e) {
            e.printStackTrace();
        }finally{
            if(cl !=null) {
                cl.Close();
            }
        }
       
       
        
        return res;
    }
    /**
     * 根据查询参数获取结果集的总条数
     * @param map
     * @return
     */
    public  int getSphinxResultCount(Map map){
        SphinxClient cl = new SphinxClient();
        String index = (String) map.get("index");
        String sortClause =  map.get("sortClause") ==null ? "" : (String)map.get("sortClause");
        String groupBy =  map.get("groupBy") == null ? "" : (String)map.get("groupBy");
        String groupSort =  map.get("groupSort") ==null ? "" : (String) map.get("groupSort") ;
        int model = (Integer) map.get("model");
        int sortModel = (Integer) map.get("sortModel");
        SphinxResult res=null;
        try {
            cl.SetServer ( host, port );
            cl.SetWeights ( new int[] { 100, 1 } );
            cl.SetMatchMode ( model );
            //cl.SetLimits ( offset, limit );
            cl.SetSortMode ( sortModel, sortClause );
            if ( groupBy.length()>0 )
                cl.SetGroupBy ( groupBy, SphinxClient.SPH_GROUPBY_ATTR, groupSort );

             res = cl.Query((String)map.get("queryStr"), index);
             if ( res==null )
             {
                 System.err.println ( "Error: " + cl.GetLastError() );
                 System.exit ( 1 );
             }
             if ( cl.GetLastWarning()!=null && cl.GetLastWarning().length()>0 )
                 System.out.println ( "WARNING: " + cl.GetLastWarning() + "\n" );
        } catch (SphinxException e) {
            e.printStackTrace();
        }finally{
            if(cl !=null) {
                cl.Close();
            }
        }
       return res.matches.length;
    }

}
